package edu.ccut.saturn.component.utils;

import java.util.ArrayList;
import java.util.List;

import edu.ccut.saturn.component.ILogger;
import edu.ccut.saturn.component.ILoggerListener;

/**
 * 日志类，封装Log4J日志对象，实现对日志的输出及级别的处理
 * 日志级别包括：info、warn、debug、error、fatal
 * 
 * @author LeslieGu
 *
 */
public class SaturnLogger implements ILogger{
	
	private String key;
	
	private String level;
	
	private static List<ILoggerListener> loggers = new ArrayList<ILoggerListener>();
	
	public static void addLoggerListener(ILoggerListener loggerListener) {
		loggers.add(loggerListener);
	}
	
	public static void removeLoggerListener(ILoggerListener loggerListener) {
		loggers.remove(loggerListener);
	}
	
	public SaturnLogger (String key) {
		this.key = key;
	}
	
	public String getKey() {
		return key;
	}

	public void debug(String message, Throwable throwable) {
		for (ILoggerListener logger : loggers) {
			logger.debug(key, message, throwable);
		}
	}

	public void debug(String message) {
		for (ILoggerListener logger : loggers) {
			logger.debug(key, message);
		}
	}

	public void error(String message, Throwable throwable) {
		for (ILoggerListener logger : loggers) {
			logger.error(key, message, throwable);
		}
	}

	public void error(String message) {
		for (ILoggerListener logger : loggers) {
			logger.error(key, message);
		}
	}

	public void fatal(String message, Throwable throwable) {
		for (ILoggerListener logger : loggers) {
			logger.fatal(key, message, throwable);
		}
	}

	public void fatal(String message) {
		for (ILoggerListener logger : loggers) {
			logger.fatal(key, message);
		}
	}

	public String getLevel() {
		return level;
	}

	public void info(String message) {
		for (ILoggerListener logger : loggers) {
			logger.info(key, message);
		}
	}

	public void setLevel(String levelStr) {
		this.level = levelStr;
		
		for (ILoggerListener logger : loggers) {
			logger.setLevel(key, level);
		}
	}

	public void warn(String message, Throwable throwable) {
		for (ILoggerListener logger : loggers) {
			logger.warn(key, message, throwable);
		}
	}

	public void warn(String message) {
		for (ILoggerListener logger : loggers) {
			logger.warn(key, message);
		}
	}

	public void info(String message, Throwable throwable) {
		for (ILoggerListener logger : loggers) {
			logger.info(key, message, throwable);
		}
	}
}
